﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace AppEnvironment.Lib
{
    [Statefull(StateTypeEnum.Thread)]
    public class LoggerProvider
    {
        public virtual void Info(string source, string location, string message)
        {
            CreateSource(source);
            EventLog.WriteEntry(source, MakeMessage(location, message), EventLogEntryType.Information);
        }

        public virtual void Error(string source, string location, string message)
        {
            CreateSource(source);
            EventLog.WriteEntry(source, MakeMessage(location, message), EventLogEntryType.Error);
        }

        public void Error(string source, string location, Exception exception)
        {
            Error(source, location, exception.ToString());
        }

        private string MakeMessage(string location, string message)
        {
            return string.Format("{0}\r\n\r\n{1}", location, message);
        }

        protected virtual void CreateSource(string source)
        {
            if (!EventLog.SourceExists(source))
            {
                EventLog.CreateEventSource(source, "Application");
            }
        }

    }
}
